package com.education_system.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.SQLException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.education_system.bean.Message;
import com.education_system.server.service.impl.AdminServiceImpl;
import com.education_system.util.IOperation;
import com.education_system.util.IType;

public class AdminThread extends Thread{
	private Socket socket;
	private ObjectOutputStream objos;
	private ObjectInputStream objis;
		public AdminThread(Socket socket){
			this.socket = socket;
		}
		public void run(){
			Message message = null;
			try {
				objos = new ObjectOutputStream(socket.getOutputStream());
				objis = new ObjectInputStream(socket.getInputStream());
				message = (Message)objis.readObject();
				AdminServiceImpl adminServiceImpl = new AdminServiceImpl();
				if(message.getType().equals(IType.ADMIN)){
					if(message.getOperationCode().equals(IOperation.LOGIN)){
						Message messages = adminServiceImpl.login(message);
						objos.writeObject(messages);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_RE_STUNOTICE)){
						Message messages = adminServiceImpl.notice(message);
						objos.writeObject(messages);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_RE_TEANOTICE)){
						Message messages = adminServiceImpl.notice(message);
						objos.writeObject(messages);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_RE_NOTICE)){
						Message messages = adminServiceImpl.notice(message);
						objos.writeObject(messages);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_ADD_STUUSER)){
						Message messages = adminServiceImpl.addStudentInfo(message);
						objos.writeObject(messages);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_ADD_TEAUSER)){
						Message messages = adminServiceImpl.addTeacherInfo(message);
						objos.writeObject(messages);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_QUERY_STUINFO)){
						Message messages = adminServiceImpl.queryStudentInfo(message);
						objos.writeObject(messages);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_QUERY_TEAINFO)){
						System.out.println("1");
						Message messages = adminServiceImpl.queryTeacherInfo(message);
						objos.writeObject(messages);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_ADD_EXAM)){
						Message fileMessage = adminServiceImpl.addExam(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_QUERY_EXAM)){
						Message fileMessage = adminServiceImpl.queryExam(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_QUERYCOLLAGE)){
						Message fileMessage = adminServiceImpl.queryCollage(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_QUERYMAJOR)){
						Message fileMessage = adminServiceImpl.queryMajor(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_QUERYCLASS)){
						Message fileMessage = adminServiceImpl.queryClass(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_QUERYSTUDENT)){
						Message fileMessage = adminServiceImpl.queryStudent(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_ADDCOLLAGE)){
						Message fileMessage = adminServiceImpl.addCollage(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_ADDMAJOR)){
						Message fileMessage = adminServiceImpl.addMajor(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_ADDCLASS)){
						Message fileMessage = adminServiceImpl.addClass(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}else if(message.getOperationCode().equals(IOperation.ROOT_QUERYTEA_COMMAND)){
						Message fileMessage = adminServiceImpl.queryTeaCommand(message);
						objos.writeObject(fileMessage);
						objos.flush();
					}
			
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
}
